package crawler.web.engine.custom;

import java.io.FileNotFoundException;

import com.sleepycat.je.DatabaseException;

import crawler.web.beans.CrawlURL;
import crawler.web.engine.BDBFrontier;
import crawler.web.engine.VisitedFrontier;

/**
 * @author waynechen Feb 24, 2012 11:31:45 AM
 */
public class TwoDBFrontier {

	private BDBFrontier frontierGet;
	private BDBFrontier frontierSet;
	private final VisitedFrontier bloomFilter;

	public TwoDBFrontier(String path, VisitedFrontier bloomFilter) throws DatabaseException, FileNotFoundException {
		this.bloomFilter = bloomFilter;
		frontierGet = new BDBFrontier(path + "/1");
		frontierSet = new BDBFrontier(path + "/2");
	}

	private synchronized void swap() {
		BDBFrontier tmp;
		tmp = frontierGet;
		frontierGet = frontierSet;
		frontierSet = tmp;
	}

	/**
	 * 过滤爬过的url
	 * 
	 * @author waynechen
	 * @param url
	 * @throws Exception
	 */
	public synchronized boolean put(CrawlURL url) throws Exception {
		if (!bloomFilter.findAndAdd(url.getOriUrl())) {
			frontierSet.put(url);
			return true;
		}
		return false;
	}

	/**
	 * 不过滤
	 * 
	 * @author waynechen
	 * @param url
	 * @throws Exception
	 */
	public synchronized void putBack(CrawlURL url) throws Exception {
		frontierSet.put(url);
	}

	public synchronized CrawlURL pop() throws Exception {
		CrawlURL url = frontierGet.pop();
		if (url == null) {
			swap();
			url = frontierGet.pop();
		}
		if (url == null) {
			swap();
			url = frontierGet.pop();
		}
		return url;
	}

	public synchronized void delete(CrawlURL url) {
		if (null == frontierGet.delete(url.getOriUrl()))
			frontierSet.delete(url.getOriUrl());
	}

}

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
